import axios from 'axios'

const baseURL = 'http://localhost:8082'

const request = axios.create({
  baseURL,
  timeout: 10000
})

// 请求拦截器
request.interceptors.request.use(
  config => {
    // 修复：为所有/organ/**请求设置Content-Type为application/json
    if (config.url && config.url.includes('/organ/')) {
      config.headers['Content-Type'] = 'application/json'
    }
    return config
  },
  error => {
    return Promise.reject(error)
  }
)

// 响应拦截器
request.interceptors.response.use(
  response => {
    return response.data
  },
  error => {
    return Promise.reject(error)
  }
)

/**
 * 获取顶级器官分类信息
 * @returns
 */
export function queryTopCategorys() {
  return request({
    url: '/organ/query/top/categorys',
    method: 'post'
  })
}

/**
 * 获取主器官下的所有属性
 * @param {object} params QueryByOrganIdReq
 * @param {number} params.organId 器官id
 * @returns
 */
export function queryParamsByMainOrganId(data) {
  return request({
    url: `/organ/query/params/by/mainOrganId`,
    method: 'post',
    data: data
  })
}

/**
 * 根据器官id查询属性信息
 * @param {object} params QueryByOrganIdReq
 * @param {number} params.organId 器官id
 * @returns
 */
export function queryParamByOrganId(data) {
  return request({
    url: `/organ/query/param/by/organId`,
    method: 'post',
    data: data
  })
}

/**
 * 根据器官id查询所有器官子分类
 * @param {object} params QueryByOrganIdReq
 * @param {number} params.organId 器官id
 * @returns
 */
export function queryCategoryByParentId(data) {
  return request({
    url: '/organ/query/category/by/parentId',
    method: 'post',
    data: data
  })
}


/**
 * 根据器官id查询所有器官分类树
 * @param {object} params QueryByOrganIdReq
 * @param {number} params.organId 器官id
 * @returns
 */
export function queryCategoryTreeByParentId(data) {
  return request({
    url: '/organ/query/category/tree/by/organId',
    method: 'post',
    data: data
  })
}



/**
 * 获取所有结构化模板信息
 * @returns
 */
export function queryAllTemplate(data) {
  return request({
    url: `/organ/query/all/templates`,
    method: 'post',
    data: data
  });
}

// 模拟获取叶子节点详细信息
function getMockDetail(organId) {
  // 模拟真实API返回的数据结构
  const mockDetails = {
    24: [
      {
        "id": 1,
        "mainOrganId": 1,
        "organId": 24,
        "name": "观察方法",
        "type": 2,
        "sort": 1,
        "showName": 1,
        "organScheme": {
          "options": [
            {"value": "NBI", "sort": 0, "checked": 0},
            {"value": "白光", "sort": 1, "checked": 0},
            {"value": "色素内镜", "sort": 2, "checked": 0}
          ],
          "units": []
        }
      },
      {
        "id": 2,
        "mainOrganId": 1,
        "organId": 24,
        "name": "溃疡位置",
        "type": 1,
        "sort": 2,
        "showName": 1,
        "organScheme": {
          "options": [
            {"value": "上段", "sort": 0, "checked": 0},
            {"value": "中段", "sort": 1, "checked": 0},
            {"value": "下段", "sort": 2, "checked": 0}
          ],
          "units": []
        }
      },
      {
        "id": 3,
        "mainOrganId": 1,
        "organId": 24,
        "name": "溃疡距门齿距离",
        "type": 3,
        "sort": 3,
        "showName": 1,
        "organScheme": {
          "options": [{"value": "", "sort": null, "checked": null}],
          "units": [{"value": "cm", "sort": null, "checked": null}]
        }
      },
      {
        "id": 4,
        "mainOrganId": 1,
        "organId": 24,
        "name": "溃疡数量",
        "type": 1,
        "sort": 4,
        "showName": 1,
        "organScheme": {
          "options": [
            {"value": "1", "sort": 0, "checked": 0},
            {"value": "2", "sort": 1, "checked": 0},
            {"value": "3", "sort": 2, "checked": 0},
            {"value": "多", "sort": 3, "checked": 0}
          ],
          "units": []
        }
      },
      {
        "id": 5,
        "mainOrganId": 1,
        "organId": 24,
        "name": "溃疡大小",
        "type": 4,
        "sort": 5,
        "showName": 1,
        "organScheme": {
          "options": [
            {"value": "", "sort": 0, "checked": null},
            {"value": "", "sort": 1, "checked": null}
          ],
          "units": [
            {"value": "cm", "sort": 0, "checked": 0},
            {"value": "mm", "sort": 1, "checked": 0}
          ]
        }
      },
      {
        "id": 6,
        "mainOrganId": 1,
        "organId": 24,
        "name": "溃疡位置",
        "type": 0,
        "sort": 6,
        "showName": 1,
        "organScheme": null
      },
      {
        "id": 7,
        "mainOrganId": 1,
        "organId": 24,
        "name": "备注",
        "type": 0,
        "sort": 7,
        "showName": 1,
        "organScheme": {
          "options": [{"value": "", "sort": null, "checked": null}],
          "units": null
        }
      }
    ],
    25: [
      {
        "id": 8,
        "mainOrganId": 1,
        "organId": 25,
        "name": "胃部检查方法",
        "type": 1,
        "sort": 1,
        "showName": 1,
        "organScheme": {
          "options": [
            {"value": "普通胃镜", "sort": 0, "checked": 0},
            {"value": "无痛胃镜", "sort": 1, "checked": 0}
          ],
          "units": []
        }
      }
    ],
    26: [
      {
        "id": 9,
        "mainOrganId": 1,
        "organId": 26,
        "name": "肠道检查备注",
        "type": 0,
        "sort": 1,
        "showName": 1,
        "organScheme": null
      }
    ]
  }
  
  return Promise.resolve({
    code: 0,
    msg: 'SUCCESS',
    data: mockDetails[organId] || []
  })
}

export { getMockDetail }